package cn.semes.org.controller;

import cn.semes.basic.config.BussinessException;
import cn.semes.basic.util.AjaxResult;
import cn.semes.basic.util.PageList;
import cn.semes.org.domain.Department;
import cn.semes.org.domain.Employee;
import cn.semes.org.query.DepartmentQuery;
import cn.semes.org.service.IDepartmentService;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dept")

public class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;

    /**
     * 查询所有zzzzzs
     *
     * @return
     */
    @GetMapping()
    public List<Department> findAll() {
        return departmentService.findAll();
    }

    /**
     * 添加或者修改
     *
     * @param department
     * @return
     */
    @PutMapping()
    public AjaxResult addOrUpdate(@RequestBody Department department) {
        if (null != department.getId()) {
            // 修改
            departmentService.update(department);
        } else {
            // 添加
            departmentService.add(department);
        }
        return AjaxResult.me().success();
    }

    /**
     * 删除
     */
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {
        departmentService.delete(id);
        return AjaxResult.me().success();
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PatchMapping()
    public AjaxResult batchDelete(@RequestBody List<Long> ids) {
        departmentService.batchDelete(ids);
        return AjaxResult.me().success();
    }
    @GetMapping("/findDeptTree")
    public List<Department> findDeptTree(){
       return departmentService.findDeptTree();
    }

    // 抽屉组件
    @GetMapping("/drawer/{id}")
    public List<Employee> findByDeptId(@PathVariable("id") Long id) {
        return departmentService.findByDeptId(id);
    }

    /**
     * 分页查询
     */
    @PostMapping()
    public PageList<Department> queryByPage(@RequestBody DepartmentQuery query) {
        return departmentService.queryByPage(query);
    }


}
